package com.lagou.dao.impl;

import com.lagou.dao.AccountDao;
import com.lagou.utils.ConnectionUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

/**
 * @author Roger
 * @date 2020/8/15 8:13
 * @description
 */
@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private QueryRunner queryRunner;

    @Autowired
    private ConnectionUtil connectionUtil;

    @Override
    public void transferTo(String to, double money) {
        String sql = "update account set money=money+? where name=?";
        try {
            queryRunner.update(connectionUtil.getConnection(),sql,money, to);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void transferFrom(String from, Double money) {
        String sql = "update account set money=money-? where name=?";

        try {
            queryRunner.update(connectionUtil.getConnection(),sql,money, from);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
